//
//  EFengShare.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/29.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengShare.h"
#import "MJExtension.h"
#import "EFengContent.h"
#import "UIUtil.h"
#import "NSString+CBExtension.h"
#import "EFengSharePhoto.h"
#import "NSArray+Extension.h"
#import "WZAttributedAction.h"


@implementation EFengShare

+ (NSDictionary *)replacedKeyFromPropertyName
{
    return @{@"shareId":@"id"};
}

//+ (NSDictionary *)objectClassInArray
//{
//    return @{@"contentReply":[EFengContent class],@"imageName":[EFengSharePhoto class]};
//}


- (NSAttributedString *)attributedText
{
    NSMutableAttributedString *atrrM = [self.content cookEmotionStringWithFont:Font16 textColor:RGBCOLORWITH16COLOR(0x323232)];
    
    return [self attributedTextWithLinksWithStr:atrrM];
}

- (NSAttributedString *)zanAttrText
{
    if (self.praiseArray.count <= 0) {
        return nil;
    }
    
    
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] init];
    
    NSTextAttachment *atta = [[NSTextAttachment alloc] init];
    atta.image = [UIImage imageNamed:@"btn-good"];
    atta.bounds = CGRectMake(0, Font13.descender, Font13.lineHeight - 2, Font13.lineHeight - 3);
    NSAttributedString *at = [NSAttributedString attributedStringWithAttachment:atta];
    [attrM appendAttributedString:at];
    [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:Font13}]];
    [self.praiseArray enumerateObjectsUsingBlock:^(EFengContent *obj, NSUInteger idx, BOOL *stop) {
        if (self.praiseArray.count - 1 == idx) {
            [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",obj.sendUserName] attributes:@{NSFontAttributeName:Font13,NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f),@"action":[[WZAttributedAction alloc] initWithAction:^() {
                [[NSNotificationCenter defaultCenter] postNotificationName:WZAttributedActionDidClick object:obj.sendUserId];
            }]}]];
           
            
        } else {
            [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:obj.sendUserName attributes:@{NSFontAttributeName:Font13,NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f),@"action":[[WZAttributedAction alloc] initWithAction:^() {
                [[NSNotificationCenter defaultCenter] postNotificationName:WZAttributedActionDidClick object:obj.sendUserId];
            }]}]];
            [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:Font13,NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f)}]];
        }
       
    }];
    return attrM;
}

//添加url
-(NSMutableAttributedString*)attributedTextWithLinksWithStr:(NSMutableAttributedString *)attrString
{
    NSMutableAttributedString* str = [attrString mutableCopy];
    if (!str) return nil;
    
    NSString* plainText = [str string];
    if (plainText )
    {
        NSError* error = nil;
        
        NSRegularExpression *urlRegex = [NSRegularExpression regularExpressionWithPattern: @"((http[s]{0,1}|ftp|items)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)?([a-zA-Z0-9\\-]+\\.)+([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://)?(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\\.((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){2}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)" options:NSRegularExpressionCaseInsensitive error:&error];
        
        
        [urlRegex enumerateMatchesInString:plainText options:0 range:NSMakeRange(0, [plainText length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            
            NSString *acStr = [plainText substringWithRange:result.range];
            
            [str removeAttribute:NSForegroundColorAttributeName range:[result range]]; // Work around for Apple leak
            [str addAttribute:NSForegroundColorAttributeName value:RGBCOLORWITH16COLOR(0x1e628f) range:[result range]];
            [str addAttribute:@"action" value:[[WZAttributedAction alloc] initWithAction:^{
                NSString *phoneRegex = @"^((https://)|(http://)|(items://)(ftp://))";
                NSRegularExpression *phoneTest = [NSRegularExpression regularExpressionWithPattern:phoneRegex options:NSRegularExpressionCaseInsensitive error:nil];
                NSTextCheckingResult *range = [phoneTest firstMatchInString:acStr options:0 range:NSMakeRange(0, acStr.length)];
                if (range) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[M_STOREVALUE(acStr) stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
                } else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[NSString stringWithFormat:@"http://%@",M_STOREVALUE(acStr)] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
                }

                
            }] range:[result range]];
        }];
        
//        NSRegularExpression *phoneNumber = [NSRegularExpression regularExpressionWithPattern: @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}" options:0 error:&error];
//        [phoneNumber enumerateMatchesInString:plainText options:0 range:NSMakeRange(0, [plainText length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
//            
//            NSString *acStr = [plainText substringWithRange:result.range];
//            
//            [str removeAttribute:NSForegroundColorAttributeName range:[result range]]; // Work around for Apple leak
//            [str addAttribute:NSForegroundColorAttributeName value:RGBCOLORWITH16COLOR(0x1e628f) range:[result range]];
//            [str addAttribute:@"action" value:[[WZAttributedAction alloc] initWithAction:^{
//                UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"%@可能是一个电话号码,你可以",acStr] delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"呼叫" otherButtonTitles:@"复制",@"保存到通讯录", nil];
//                [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
//
//                
//            }] range:[result range]];
//            
//        }];
        
    }
    return str;
}


- (NSString *)description
{
    return [NSString stringWithFormat:@"%@_%@_%@_%@_%@_%@_%@_%@_%@_%@_%@_%@_%@_%@_%@",M_STOREVALUE(self.shareId), M_STOREVALUE(self.releaseUserName), M_STOREVALUE(self.releaseUserId),M_STOREVALUE(self.visibleUserId), M_STOREVALUE(self.praiseUserId), M_STOREVALUE(self.praiseCount), M_STOREVALUE(self.releaseType), M_STOREVALUE([self.imageName jsonString]), M_STOREVALUE(self.content), M_STOREVALUE(self.companyId), M_STOREVALUE(self.date),self.isRead?@1:@0,self.isClick?@1:@0,M_STOREVALUE(self.yuLiuZiduan1),@(self.yuLiuZiduan2)];
}
@end
